<template>
  <view
    class="oto-lazy-load-image"
    :id="id"
    :style="[
      {
        minHeight: loading ? '320rpx' : 'auto'
      }
    ]"
  >
    <image
      class="loaded-animation"
      :class="{
        'show-animation': showAnimation
      }"
      :style="[getStyle]"
      :src="src + zoom"
      :mode="getMode"
      :lazy-load="lazyLoad"
      v-show="show"
      @transitionend="over"
      @load="loaded"
      @error="loadError"
    />
    <view :style="[getStyle]" class="oto-lazy-load-image-loading loaded-animation" v-if="loading">{{
      loadingText
    }}</view>
  </view>
</template>

<script>
  import { guid } from '@/utils/math';
  const loadStatusEnum = {
    NOT_LOAD: 'not-load',
    LOADED: 'loaded'
  };

  export default {
    props: {
      src: {
        type: String,
        default: ''
      },
      zoom: {
        type: String,
        default: '?x-oss-process=image/quality,Q_15'
      },
      size: {
        type: String,
        default: '?x-oss-process=image/info'
      },
      mode: {
        type: String,
        default: 'widthFix'
      },
      customStyle: {
        type: Object,
        default() {
          return {};
        }
      },
      lazyLoad: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        style: {},
        show: false,
        id: guid(),
        loading: true,
        showAnimation: false,
        nowMode: '',
        loadStatus: loadStatusEnum.NOT_LOAD,
        loadingText: '加载中...'
      };
    },
    computed: {
      getStyle() {
        const _style = {};
        Object.assign(_style, this.style, this.customStyle);
        return _style;
      },
      getMode() {
        let _mode = this.mode;
        if (this.nowMode) {
          _mode = this.nowMode;
        }
        return _mode;
      }
    },
    methods: {
      async loaded(e) {
        // 真正的图片加载完成
        const { width, height } = e.detail;
        const res = uni.getSystemInfoSync();
        const { windowWidth, windowHeight } = res;
        const line = width / height;
        const transformHeight = windowWidth / line;
        this.show = true;
        setTimeout(() => {
          this.$emit('loaded');
        }, 100);
        // 判断是否是超长图，限制其大小显示
        if (windowHeight * 1.1 - transformHeight < 0) {
          this.nowMode = 'aspectFill';
          this.$set(this.style, 'height', `${windowHeight * 0.5}px`);
          setTimeout(() => {
            this.over();
          }, 150);
        }
      },
      loadError() {
        this.loadingText = '加载失败';
      },
      showAnimationFun() {
        setTimeout(() => {
          this.showAnimation = false;
        }, 500);
      },
      over() {
        this.loading = false;
        this.showAnimation = true;
        this.showAnimationFun();
      }
    }
  };
</script>

<style lang="scss" scoped>
  .loaded-animation {
    transition: height 0.1s ease;
  }

  @keyframes showAnimation {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }

  .show-animation {
    animation: showAnimation 0.5s ease-in-out;
  }

  .oto-lazy-load-image {
    position: relative;

    image {
      width: 100%;
      border-radius: 12rpx;
    }
    &-loading {
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #c0c0c0;
      background-color: #ebebeb;
    }
  }
</style>
